<template>
    <div>


        <el-breadcrumb separator="/" >
            <el-breadcrumb-item :to="{ path: '/test' }" active-text-color="#ffd04b">首页</el-breadcrumb-item>

            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
<!--    主体部分-->
        <el-card  class="mainn">

            <el-row :gutter="25">
                <el-col :span="10">
                    <!-- 搜索添加 -->
                  <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getFindUserList">
                    <el-button slot="append" icon="el-icon-search" @click="getFindUserList"></el-button>
                  </el-input>
                </el-col>
              <el-col :span="4">
                <el-button type="primary" @click="addDialogVisible = true">添加用户</el-button>
              </el-col>
            </el-row>
            <!-- 用户列表 -->
            <el-table  width="180" :data="userlist"  style="hight: 100px "
                      border stripe>
                <el-table-column type="index" ></el-table-column>
                <el-table-column label="姓名" prop="username"></el-table-column>

                <el-table-column label="密码" prop="password"></el-table-column>
                <el-table-column label="角色" prop="role"></el-table-column>
              <el-table-column label="车牌号" prop="carnumber"></el-table-column>
<!--                <el-table-column label="状态" prop="state">-->

<!--&lt;!&ndash;                     scope.row 就是当前行的信息 &ndash;&gt;-->
<!--                    <template slot-scope="scope">-->
<!--&lt;!&ndash;                        {{scope.row}}}&ndash;&gt;-->
<!--                        <el-switch v-model="scope.row.state" @change="userStateChange(scope.row)"></el-switch>-->
<!--                    </template>-->
<!--                </el-table-column>-->

                <el-table-column label="操作">
                    <template slot-scope="scope">
                      <!-- 修改 -->
                      <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.username)"></el-button>
                      <!-- 删除 -->
                      <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUser(scope.row.username)"></el-button>
                      <!-- 权限 -->
                      <el-tooltip effect="dark" content="分配权限" placement="top-start" :enterable="false">
                            <el-button type="warning" icon="el-icon-setting" size="mini" @click="choosequanxian(scope.row)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
          <div>

          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pageNum"
            :page-sizes="[1, 2, 5, 100]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
          </div>
        </el-card>

      <!-- 创建新用户对话框 -->
      <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%"
                 @close="addDialogClosed">
        <!-- 内容主体区域 -->
        <el-form :model="addForm" :rules="addFormRules"
                 ref="addFormRef" label-width="70px">
          <!-- 用户名 -->
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <!-- 邮箱 -->

          <el-form-item label="车牌号" prop="email">
            <el-input v-model="addForm.carnumber"></el-input>
          </el-form-item>
        </el-form>
        <!-- 内容底部区域 -->
        <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
      </el-dialog>
      <!-- 修改用户对话框 -->
      <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="50%" @colse="editDialogClosed">
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
          <!-- 用户名 -->
          <el-form-item label="用户名" prop="username" >
            <el-input v-model="editForm.username" disabled></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input v-model="editForm.password"></el-input>
          </el-form-item>
          <!-- 邮箱 -->

          <el-form-item label="车牌号" prop="carnumber">
            <el-input v-model="editForm.carnumber"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
      </el-dialog>
    </div>
</template>

<script>
export default {
  created (){
    this.getUserList();
  },
        data(){
            return{
                //查询信息实体
                queryInfo:{
                    query:"",//查询信息
                    pageNum: 1,//当前页
                    pageSize: 5,//每页最大数
                },
                userlist:[],//用户列表
                total:0,//总记录数
              addDialogVisible: false,
              // 添加用户表单项
              addForm: {
                username:'',
                password:'',

                carnumber:'',
              },


              // 控制修改用户对话框显示/隐藏
              editDialogVisible:false,
              // 修改用户信息
              editForm:{},
              // 验证规则
              addFormRules:{
                // 添加用户表单验证规则
                username:[
                  { required: true, message: "请输入用户名", trigger: "blur" },
                  { min: 1, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
                ],
                password:[
                  { required: true, message: "请输入密码", trigger: "blur" },
                  { min: 1, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
                ],

              },
              // 修改用户表单验证规则
              editFormRules:{
                password:[
                  { required: true, message: "请输入密码", trigger: "blur" },
                  { min: 6, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
                ],

              },
            }

        },
        methods:{
            async getUserList() {
                // 调用post请求
              //   const { data: res } = await this.$http.get("txt");
              //
              //   this.userlist = res.data; // 将返回数据赋值
              //   // this.total = res.numbers; // 总个数
              // console.log(123);



              // 调用post请求
              const { data: res } = await this.$http.get("txt", {
                params: this.queryInfo
              });
              this.userlist = res.data; // 将返回数据赋值
              this.total = res.numbers; // 总个数
              console.log(total);



            },

          // 监听pageSize改变的事件
          handleSizeChange(newSize) {
            this.queryInfo.pageSize = newSize;
            this.getUserList(); // 数据发生改变重新申请数据
          },
          // 监听pageNum改变的事件
          handleCurrentChange(newPage) {
            this.queryInfo.pageNum = newPage;
            this.getUserList(); // 数据发生改变重新申请数据
          },
          async getFindUserList() {
            // 调用post请求
            const { data: res } = await this.$http.get("txtfind", {
              params: this.queryInfo
            });
            this.userlist = res.data; // 将返回数据赋值
            // this.total = res.numbers; // 总个数
           // console.log(queryInfo);



          },
          async addDialogClosed(){
              this.$refs.addFormRef.resetFileds();


          },
          async choosequanxian(user){
            // 弹框

            const confirmResult = await this.$confirm('此操作将更改该用户权限, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(err => err)
            // 成功删除为confirm 取消为 cancel
            if(confirmResult!='confirm'){
              return this.$message.info("已取消删除");
            }
            const {data:res} = await this.$http.post("choosequanxian",user);
            if (res != "success") {
              return this.$message.error("操作失败！！！");
            }
            this.$message.success("操作成功！！！");
            this.getUserList();
          },
          async deleteUser(username){
            // 弹框
            console.log(username);
            const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(err => err)
            // 成功删除为confirm 取消为 cancel
            if(confirmResult!='confirm'){
              return this.$message.info("已取消删除");
            }
            const {data:res} = await this.$http.delete("deleteUser?username="+username);
            if (res != "success") {
              return this.$message.error("操作失败！！！");
            }
            this.$message.success("操作成功！！！");
            this.getUserList();
          },

          async addUser(){
            this.$refs.addFormRef.validate(async valid=>{
              if( !valid ) return;
              // 发起请求
              const {data:res} = await this.$http.post("addUser",this.addForm);

              if (res != "success") {
                userinfo.state = !userinfo.state;
                return this.$message.error("你已经注册过了");
              }
              // this.$message.success("操作成功！！！");
              //隐藏
              this.addDialogVisible = false;
              this.getUserList();
            })
          },
          async userStateChange(userinfo) {
              console.log(userinfo.username)
            console.log(userinfo.state)
            const { data: res } = await this.$http.post(
              `userState?username=${userinfo.username}&state=${userinfo.state}`
            );
         //    const { data: res } = await this.$http.get("userState");
         // console.log(res)
          //
            if (res != "success") {
              // userinfo.state = !userinfo.state;
              return this.$message.error("操作失败！！！");
            }
            this.$message.success("操作成功！！！");


            },


          // 展示修改框
          async showEditDialog(username){

            const {data:res} = await this.$http.get("getUpdate?username="+username);
            // if (res != "success") {
            // userinfo.state = !userinfo.state;
            // return this.$message.error("操作失败！！！");
            // }
            // this.$message.success("操作成功！！！");

            this.editForm = res;
            this.editDialogVisible = true;//编辑菜单开启
          },
          // 关闭窗口,重置信息
          editDialogClosed(){
            this.$refs.editFormRef.resetFields();
          },
          // 确认修改
          editUserInfo(){
            this.$refs.editFormRef.validate(async valid =>{//表单验证
              console.log(valid);
              if( !valid ) return;
              // 发起请求
              const {data:res} = await this.$http.put("editUser",this.editForm);
              console.log(res);
              if (res != "success") return this.$message.error("操作失败！！！");
              this.$message.success("操作成功！！！");
              //隐藏
              this.editDialogVisible = false;
              this.getUserList();
            });
          },
        }

    }
</script>

<style lang="less" scoped>
.el-breadcrumb{


        font-size: 17px;
    position: fixed;
    top: 0px;

}
   .shuru{
       width: 100%;
   }
    .ssbut{
        background-color: #42b983;
        position: fixed;
        top: 120px;
        right: 450px;

    }
    .el-table-column{
        height: 100px;
        width: 100px;
        background-color: #42b983;
    }
</style>
